package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanZiMQService;
import com.tanhua.server.vo.MovementVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("movements")
@RestController
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanZiMQService quanZiMQService;

    /**
     * 发布动态
     *
     * @param textContent
     * @param location
     * @param latitude
     * @param longitude
     * @param multipartFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> savePublish(
            @RequestParam(value = "textContent") String textContent,
            @RequestParam(value = "location", required = false) String location,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "imageContent", required = false) MultipartFile[] multipartFile
    ) {
        try {
            String publishId = movementsService.savePublish(textContent, location, latitude, longitude, multipartFile);
            if (StringUtils.isNotEmpty(publishId)) {
                quanZiMQService.publishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看好友动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize
    ) {
        try {
            PageResult pageResult = movementsService.queryUserPublishList(page, pagesize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看推荐动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize
    ) {
        try {
            PageResult pageResult = movementsService.queryRecommendPublishList(page, pagesize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> saveLikeComment(@PathVariable("id") String publishId) {
        try {
            Long likeCount = movementsService.saveLikeComment(publishId);
            if (likeCount != null) {
                quanZiMQService.likePublishMsg(publishId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消点赞
     *
     * @param publishId 发布动态的id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> removeLikeComment(@PathVariable("id") String publishId) {
        try {
            Long likeCount = movementsService.removeLikeComment(publishId);
            if (likeCount != null) {
                quanZiMQService.disLikePublishMsg(publishId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> saveLoveComment(@PathVariable("id") String publishId) {
        try {
            Long likeCount = movementsService.saveLoveComment(publishId);
            if (likeCount != null) {
                quanZiMQService.lovePublishMsg(publishId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消喜欢
     *
     * @param publishId 发布动态的id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> removeLoveComment(@PathVariable("id") String publishId) {
        try {
            Long likeCount = movementsService.removeLoveComment(publishId);
            if (likeCount != null) {
                quanZiMQService.disLovePublishMsg(publishId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看单条动态
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<MovementVo> queryMovementsById(@PathVariable("id") String publishId) {
        try {
            MovementVo movementVo = movementsService.queryMovementsById(publishId);
            if (movementVo != null) {
                //发送消息
                this.quanZiMQService.queryPublishMsg(publishId);
                return ResponseEntity.ok(movementVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList() {

        try {
            List<VisitorsVo> visitorsVoList = this.movementsService.queryVisitorsList();
            if (visitorsVoList != null) {
                return ResponseEntity.ok(visitorsVoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看个人相册动态信息
     *
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryMovementsList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.movementsService.queryMovementsList(page,pageSize,userId);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
